<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java垃圾收集器内存管理策略</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 2px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border-color: #667eea;
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 80%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .icon-box {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            font-size: 24px;
            margin-bottom: 1rem;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center animate-fadeInUp">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">Java 垃圾收集器内存管理策略</h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                深入理解新生代与老年代的划分原理，掌握高效内存管理的核心机制
            </p>
            <div class="mt-10 flex justify-center space-x-8">
                <div class="text-center">
                    <i class="fas fa-memory text-4xl mb-2"></i>
                    <p class="text-lg">内存优化</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-tachometer-alt text-4xl mb-2"></i>
                    <p class="text-lg">性能提升</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-recycle text-4xl mb-2"></i>
                    <p class="text-lg">智能回收</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-16">
        <!-- Introduction -->
        <div class="bg-white rounded-2xl shadow-xl p-10 mb-12 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">J</span>ava 的垃圾收集器将堆内存划分为新生代和老年代，主要是为了优化垃圾回收的效率和性能，使得垃圾收集器能够根据对象的生命周期特征采取不同的回收策略，从而优化了内存管理的效率，提升了应用程序的整体性能。
            </p>
        </div>

        <!-- Memory Structure Visualization -->
        <div class="bg-white rounded-2xl shadow-xl p-10 mb-12">
            <h2 class="text-3xl font-bold mb-8 text-center text-gradient">Java 堆内存结构图</h2>
            <div class="mermaid">
                graph TB
                    A[Java 堆内存] --> B[新生代 Young Generation]
                    A --> C[老年代 Old Generation]
                    B --> D[Eden 区]
                    B --> E[Survivor 0]
                    B --> F[Survivor 1]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#ed8936,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#48bb78,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#4299e1,stroke:#fff,stroke-width:2px,color:#fff
                    style F fill:#4299e1,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Section 1: Object Lifecycle -->
        <div class="bg-white rounded-2xl shadow-xl p-10 mb-12 card-hover">
            <div class="icon-box">
                <i class="fas fa-clock"></i>
            </div>
            <h3 class="text-2xl font-bold mb-6 section-title">对象的生命周期特征</h3>
            <p class="text-lg leading-relaxed text-gray-700 indent-8">
                在 Java 程序中，大部分对象的生命周期非常短暂。也就是说，它们通常在创建后不久就变得无用。因此，Java 的垃圾收集器设计时考虑到了这种特性。通过将堆内存划分为新生代和老年代，垃圾收集器可以更有效地处理这些短暂和长期存活的对象。
            </p>
        </div>

        <!-- Section 2: Young Generation -->
        <div class="bg-white rounded-2xl shadow-xl p-10 mb-12 card-hover">
            <div class="icon-box">
                <i class="fas fa-baby"></i>
            </div>
            <h3 class="text-2xl font-bold mb-6 section-title">新生代（Young Generation）</h3>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                <strong class="text-purple-600">新生代</strong>主要用于存储新创建的对象。新生代又分为三个区域：
            </p>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-gradient-to-br from-green-50 to-green-100 p-6 rounded-xl">
                    <div class="flex items-center mb-3">
                        <i class="fas fa-seedling text-green-600 text-2xl mr-3"></i>
                        <h4 class="text-xl font-bold text-green-800">Eden 区</h4>
                    </div>
                    <p class="text-gray-700">新创建的对象首先被分配到 Eden 区。</p>
                </div>
                
                <div class="bg-gradient-to-br from-blue-50 to-blue-100 p-6 rounded-xl">
                    <div class="flex items-center mb-3">
                        <i class="fas fa-shield-alt text-blue-600 text-2xl mr-3"></i>
                        <h4 class="text-xl font-bold text-blue-800">Survivor 区</h4>
                    </div>
                    <p class="text-gray-700">（S0 和 S1）：如果对象在 Eden 区经历了一次垃圾回收后仍然存活，它们会被移动到 Survivor 区。这两个 Survivor 区轮流使用。</p>
                </div>
            </div>
            
            <p class="text-lg leading-relaxed text-gray-700 mt-6 indent-8">
                新生代的垃圾回收（称为 Minor GC）频率较高，但每次回收的工作量相对较小，因为它主要回收那些生命周期短的对象。新生代的垃圾回收使用的是复制算法，将存活的对象复制到 Survivor 区或老年代，这样可以避免碎片化的问题，并且回收过程较快。
            </p>
        </div>

        <!-- Section 3: Old Generation -->
        <div class="bg-white rounded-2xl shadow-xl p-10 mb-12 card-hover">
            <div class="icon-box">
                <i class="fas fa-history"></i>
            </div>
            <h3 class="text-2xl font-bold mb-6 section-title">老年代（Old Generation）</h3>
            <p class="text-lg leading-relaxed text-gray-700 indent-8">
                <strong class="text-orange-600">老年代</strong>用于存储那些经过多次垃圾回收仍然存活的对象。这些对象通常生命周期较长，不容易变得无用。因此，老年代的垃圾回收（称为 Full GC）比较少发生，但每次回收的工作量较大，因为它涉及到整个老年代的对象。
            </p>
            <p class="text-lg leading-relaxed text-gray-700 mt-4 indent-8">
                老年代的垃圾回收使用的是标记-清除或标记-整理算法，这些算法更适合处理存活时间较长的对象，并且可以减少内存碎片。
            </p>
        </div>

        <!-- GC Process Visualization -->
        <div class="bg-white rounded-2xl shadow-xl p-10 mb-12">
            <h2 class="text-3xl font-bold mb-8 text-center text-gradient">垃圾回收流程图</h2>
            <div class="mermaid">
                graph LR
                    A[新对象创建] --> B[分配到 Eden 区]
                    B --> C{Minor GC}
                    C -->|存活| D[移动到 Survivor 区]
                    C -->|死亡| E[回收内存]
                    D --> F{多次 Minor GC}
                    F -->|年龄达到阈值| G[晋升到老年代]
                    F -->|继续存活| D
                    G --> H{Full GC}
                    H -->|存活| G
                    H -->|死亡| I[回收内存]
                    
                    style A fill:#48bb78,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style F fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style H fill:#ed8936,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Section 4: Performance Optimization -->
        <div class="bg-white rounded-2xl shadow-xl p-10 mb-12 card-hover">
            <div class="icon-box">
                <i class="fas fa-rocket"></i>
            </div>
            <h3 class="text-2xl font-bold mb-6 section-title">性能优化</h3>
            <p class="text-lg leading-relaxed text-gray-700 mb-8">
                将堆分为新生代和老年代可以显著提高垃圾回收的效率：
            </p>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-gradient-to-br from-purple-50 to-purple-100 p-8 rounded-xl transform hover:scale-105 transition-transform duration-300">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-chart-line text-purple-600 text-3xl mr-4"></i>
                        <h4 class="text-xl font-bold text-purple-800">减少回收频率</h4>
                    </div>
                    <p class="text-gray-700 leading-relaxed">
                        新生代的垃圾回收频繁且高效，适合处理短生命周期对象。而老年代的垃圾回收不那么频繁，但处理过程较为复杂和耗时。通过将两者分开，可以优化回收策略，使得垃圾回收过程更加高效。
                    </p>
                </div>
                
                <div class="bg-gradient-to-br from-orange-50 to-orange-100 p-8 rounded-xl transform hover:scale-105 transition-transform duration-300">
                    <div class="